import api from '@/api';

export default {
  namespace: 'specialline',

  state: {
    ruleList: {
      list: [],
      pagination: {},
    },
    ruleDetail: {},
    statusList: [],
  },

  effects: {
    *queryList({ payload }, { call, put }) {
      const res = yield call(api.specialline.queryList, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveRuleList',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *delete({ payload }, { call }) {
      return yield call(api.specialline.delete, payload);
    },
    *update({ payload }, { call }) {
      return yield call(api.specialline.update, payload);
    },
    *create({ payload }, { call }) {
      return yield call(api.specialline.create, payload);
    },
    *enable({ payload }, { call }) {
      return yield call(api.specialline.enable, payload);
    },
    *disable({ payload }, { call }) {
      return yield call(api.specialline.disable, payload);
    },
    *queryStatusList({ payload }, { call, put }) {
      const res = yield call(api.specialline.queryStatusList, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveStatusList',
          payload: res.data,
        });
      }
    },
    *queryDetail({ payload }, { call, put }) {
      const res = yield call(api.specialline.queryDetail, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveRuleDetail',
          payload: res.data,
        });
      }

      return res;
    },
  },

  reducers: {
    saveRuleList(state, { payload }) {
      return {
        ...state,
        ruleList: payload,
      };
    },
    saveRuleDetail(state, { payload }) {
      return {
        ...state,
        ruleDetail: payload,
      };
    },
    saveStatusList(state, { payload }) {
      return {
        ...state,
        statusList: payload,
      };
    },
  },
};
